#encoding: utf-8

import requests
from lxml import etree
from urllib import request,error
import os
import re

def parse_page(url):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0',
        'Host': 'www.doutula.com'
    }
    response = requests.get(url, headers=headers)
    text = response.text
    print(text)
    html = etree.HTML(text)
    imgs = html.xpath("//div[@class='page-content text-center']//img[@class!='gif']")
    for img in imgs:
        img_url = img.get('data-original')
        alt = img.get('alt')
        alt = re.sub(r'[\?？\.。,，!！;:；：\'\"\"‘“”\*]', '', alt)
        suffix = os.path.splitext(img_url)[1]
        filename = alt + suffix
        try:
            request.urlretrieve(img_url, 'images/' + filename)
            print(filename + '  下载完成！')
        except error.HTTPError as e:
            print(e)
        break


def main():
    for x in range(1,101):
        url = 'http://www.doutula.com/photo/list/?page=%d' % x
        parse_page(url)
        break

if __name__ == '__main__':
    main()